﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization.Json;
using System.IO;

namespace EmailSender
{
    public partial class EventSet : Form
    {
        public string HttpGetUri
        {
            get { return txtHttpGetUri.Text.Trim(); }
            set { txtHttpGetUri.Text = value; }
        }
        public bool EnableHttpGet
        {
            get { return chkHttpGet.Checked; }
            set { chkHttpGet.Checked = value; }
        }

        public EventSet()
        {
            InitializeComponent();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model.EmailSet));
            string path = Application.StartupPath + "\\senderConfig.ini";
            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    Model.EmailSet emailSet = serializer.ReadObject(fs) as Model.EmailSet;
                    if (null != emailSet)
                    {
                        HttpGetUri = emailSet.HttpGetEventUri;
                        EnableHttpGet = emailSet.EnableHttpGetEvent;
                    }
                }
            }

        }

        private void EventSet_FormClosing(object sender, FormClosingEventArgs e)
        {
            Model.EmailSet emailSet = null;
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model.EmailSet));
            string path = Application.StartupPath + "\\senderConfig.ini";
            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    emailSet = serializer.ReadObject(fs) as Model.EmailSet;
                }
            }
            if (null == emailSet)
                emailSet = new Model.EmailSet();
            emailSet.HttpGetEventUri = HttpGetUri;
            emailSet.EnableHttpGetEvent = EnableHttpGet;
            using (FileStream fs = new FileStream(path, FileMode.Truncate))
            {
                serializer.WriteObject(fs, emailSet);
                fs.Flush();
                fs.Close();
            }
        }
    }
}
